package com.cn.txx.controller;

import java.util.List;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cn.txx.entity.SjzNd;
import com.cn.txx.entity.User;
import com.cn.txx.service.LoginService;
import com.cn.txx.utils.RedisUtils;

@RequestMapping("/login")
@Controller
public class LoginController {

	public static final Logger LOGGER  = Logger.getLogger(LoginController.class);

	@Resource
	private LoginService loginService;
	
	@RequestMapping("/test.do")
	@ResponseBody
	public User TestData(String id){
		return loginService.queryUserById(id);
	}
	
	@RequiresRoles("admin")
	@RequestMapping("/getSjzData.do")
	@ResponseBody
	public List<SjzNd> getSjzData(){
		return loginService.getSjzData();
	}

	@RequestMapping("/loginin.do")
	@ResponseBody
	public int loginin(String account,String password){
		Subject subject = SecurityUtils.getSubject();
		UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(account,password);
		try {
			subject.login(usernamePasswordToken);
		}catch (Exception e){
			LOGGER.info("验证失败，用户名或密码错误",e);
			return 0;
		}
		return 1;
	}
	public static void main(String[] args) {
		Md5Hash md5Hash = new Md5Hash("123456","");
		System.out.println(md5Hash.toString());
	}
}
